/**
    * Facebook发布REELS
    * TODO:将上传的文本修改为正式文本，此操作需要向后端请求数据，或者读取文本
    */
"app"
// 导入函数
// const config = require("/sdcard/Download/js/config.js");
// const { randomSleep, deleteDirCache, deviceWidth, deviceHeight,jsDir, getTranslateTitle,
//     _deviceModel,waitForAndClick, waitForAndSetText, exitAppAndJs,httpUploadErrMsg
// } = require("/sdcard/Download/js/config.js");
const { randomSleep, deleteDirCache, deviceWidth, deviceHeight,jsDir,getRandomSong, getTranslateTitle,
    waitForAndClick, waitForAndSetText, exitAppAndJs,httpUploadErrMsg, device_model
} = require("/sdcard/Download/js/config.js");
device.wakeUp()
const appName = "Facebook"
try{
    // const config = require("/sdcard/Download/js/config.js");

    launchApp(appName);

    /**
     * 点击创建，并点击REELS
     */
    {
        randomSleep();
        if(waitForAndClick(descContains("选择特效"),3, desc("返回"))){
            toast("有选择特效")
        }
        if(waitForAndClick(text("Create post"),3, desc("Back"))){
            toast("有草稿")
            waitForAndClick(descContains("Discard post"), 2)
        }
        toast("点击照片/视频")
        if (!waitForAndClick(descContains("Photo/Video"))) {
            exitAppAndJs("出错脚本："+appName+"\n问题："+"点击上传失败！");
        }
    }

    /**
     * (可能弹出)弹出设置REELS
     */ 
    {
        toast("检查是否有需确认")
        waitForAndClick(descContains("Confirm"),3);
    }

    /**
     * 多选资源
     */
    {
        toast("点击多选")
        if (!waitForAndClick(descContains("Select multiple"))) {
            exitAppAndJs("出错脚本："+appName+"\n问题："+"点击多选失败！");
        }
        
        toast("多选资源")
        var list = className("GridView").findOne().children();
        list.each(function(item) {
            if (item.className().includes("Button")) {
                click(item.bounds().centerX(), item.bounds().centerY());
            }
            randomSleep();
        });
        toast("下一步")
        if (!waitForAndClick(descContains("NEXT"))) {
            exitAppAndJs("出错脚本："+appName+"\n问题："+"点击下一步失败！");
        }
    }   

    /**
     * 设置文本，点击分享
     * TODO：将测试文本替换为正式文本
     */
    {
        var strText = getTranslateTitle();
        
        randomSleep();

        // 注意：depth(10)不稳定
        // if (!waitForAndClick(id("(name removed)").className("android.widget.FrameLayout").depth(10))) {
        //     exitAppAndJs("出错脚本："+appName+"\n问题："+"");
        // }
        toast("设置文本")
        if(device_model === "9"){
            log("m9")
            toast("这是m9")
            if (!waitForAndSetText(className("AutoCompleteTextView"), strText)) {
                exitAppAndJs("出错脚本："+appName+"\n问题："+"m9设置文本失败！");
            }
        }else{
            log("m5")
            toast("这是m5")
            if (!waitForAndSetText(className("EditText"), strText)) {
                exitAppAndJs("出错脚本："+appName+"\n问题："+"m5设置文本失败！");
            }
        }
        log("退出设置文本")
        toast("点击发布")
        if (!waitForAndClick(descContains("POST"))) {
            exitAppAndJs("出错脚本："+appName+"\n问题："+"点击发布失败！");
        }
    }

    /**
     * (可能弹出)若有以后再说，则点击
     */
    {
        randomSleep();
        toast("检查是否有以后再说")
        waitForAndClick(desc("以后再说"), 2, desc("以后再说"));
    }

    /**
     * 若正在上传，则等待上传结束
     */
    {
        while (descContains("Posting").exists()) {
            sleep(5000);
            toastLog("未上传结束");
        }
    }

    /**
     * 删除多余资源
     */
    {
        deleteDirCache(appName);
    }

    /**
     * 写入成功执行的文件
     */
    {
        files.createWithDirs(jsDir + "finish")
    }


    // 关闭后台
    exitAppAndJs();
}catch(e){
    httpUploadErrMsg("脚本执行错误："+appName+" => "+e)
}